package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PUserExtractBo;
import com.ruoyi.system.domain.vo.PUserExtractVo;
import com.ruoyi.system.domain.PUserExtract;
import com.ruoyi.system.mapper.PUserExtractMapper;
import com.ruoyi.system.service.IPUserExtractService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户提现Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-26
 */
@RequiredArgsConstructor
@Service
public class PUserExtractServiceImpl implements IPUserExtractService {

    private final PUserExtractMapper baseMapper;

    /**
     * 查询用户提现
     */
    @Override
    public PUserExtractVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户提现列表
     */
    @Override
    public TableDataInfo<PUserExtractVo> queryPageList(PUserExtractBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PUserExtract> lqw = buildQueryWrapper(bo);
        Page<PUserExtractVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户提现列表
     */
    @Override
    public List<PUserExtractVo> queryList(PUserExtractBo bo) {
        LambdaQueryWrapper<PUserExtract> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PUserExtract> buildQueryWrapper(PUserExtractBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PUserExtract> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, PUserExtract::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), PUserExtract::getPhone, bo.getPhone());
        lqw.like(StringUtils.isNotBlank(bo.getRealName()), PUserExtract::getRealName, bo.getRealName());
        lqw.eq(StringUtils.isNotBlank(bo.getExtractType()), PUserExtract::getExtractType, bo.getExtractType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCode()), PUserExtract::getBankCode, bo.getBankCode());
        lqw.eq(StringUtils.isNotBlank(bo.getBankAddress()), PUserExtract::getBankAddress, bo.getBankAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getAlipayCode()), PUserExtract::getAlipayCode, bo.getAlipayCode());
        lqw.eq(bo.getExtractPrice() != null, PUserExtract::getExtractPrice, bo.getExtractPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), PUserExtract::getMark, bo.getMark());
        lqw.eq(StringUtils.isNotBlank(bo.getFailMsg()), PUserExtract::getFailMsg, bo.getFailMsg());
        lqw.eq(bo.getStatus() != null, PUserExtract::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getWechat()), PUserExtract::getWechat, bo.getWechat());
        lqw.eq(bo.getFailTime() != null, PUserExtract::getFailTime, bo.getFailTime());
        lqw.like(StringUtils.isNotBlank(bo.getBankName()), PUserExtract::getBankName, bo.getBankName());
        lqw.eq(StringUtils.isNotBlank(bo.getQrcodeUrl()), PUserExtract::getQrcodeUrl, bo.getQrcodeUrl());
        return lqw;
    }

    /**
     * 新增用户提现
     */
    @Override
    public Boolean insertByBo(PUserExtractBo bo) {
        PUserExtract add = BeanUtil.toBean(bo, PUserExtract.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户提现
     */
    @Override
    public Boolean updateByBo(PUserExtractBo bo) {
        PUserExtract update = BeanUtil.toBean(bo, PUserExtract.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PUserExtract entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户提现
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insertFont(PUserExtractBo bo) {
        PUserExtract add = BeanUtil.toBean(bo, PUserExtract.class);
        // 设置后台审核
        add.setStatus("1");
        add.setUserId(LoginHelper.getUserId());
        baseMapper.insert(add);
    }

    @Override
    public TableDataInfo<PUserExtract> getWithdrawalDetail(PageQuery pageQuery) {
        List<PUserExtract> list = this.baseMapper.selectCustom(pageQuery.build(), new LambdaQueryWrapper<PUserExtract>().eq(PUserExtract::getUserId, LoginHelper.getUserId())).getRecords();
        return TableDataInfo.build(list);
    }
}
